package com.gzc.just.play.last.war.sceneserver.network.broadcast;

import com.google.protobuf.Message;
import java.io.IOException;

/**
 * 战斗玩家TCP连接 - 管理与GateServer之间的TCP连接
 * 
 * 映射自C++ ISrvCliSession 和相关连接类
 * 用于发送消息给客户端
 * 
 * @author AI编程助手
 * @version 1.0
 */
public abstract class BattlePlayerConnection {
    
    protected long playerId;
    protected long battleId;
    protected int teamId;
    protected String sessionId;
    protected volatile boolean connected = true;
    
    /**
     * 构造函数
     * 
     * @param playerId 玩家ID
     * @param battleId 战斗ID
     * @param teamId 队伍ID
     * @param sessionId 会话ID
     */
    public BattlePlayerConnection(long playerId, long battleId, int teamId, String sessionId) {
        this.playerId = playerId;
        this.battleId = battleId;
        this.teamId = teamId;
        this.sessionId = sessionId;
    }
    
    /**
     * 获取玩家ID
     * 
     * @return 玩家ID
     */
    public long getPlayerId() {
        return playerId;
    }
    
    /**
     * 获取战斗ID
     * 
     * @return 战斗ID
     */
    public long getBattleId() {
        return battleId;
    }
    
    /**
     * 获取队伍ID
     * 
     * @return 队伍ID
     */
    public int getTeamId() {
        return teamId;
    }
    
    /**
     * 获取会话ID
     * 
     * @return 会话ID
     */
    public String getSessionId() {
        return sessionId;
    }
    
    /**
     * 检查连接是否有效
     * 
     * @return 是否连接
     */
    public boolean isConnected() {
        return connected;
    }
    
    /**
     * 设置连接状态
     * 
     * @param connected 是否连接
     */
    public void setConnected(boolean connected) {
        this.connected = connected;
    }
    
    /**
     * 发送Protobuf消息
     * 
     * @param message Protobuf消息
     * @throws IOException 如果发送失败
     */
    public abstract void sendMessage(Message message) throws IOException;
    
    /**
     * 发送广播消息
     * 
     * @param broadcastMessage 广播消息
     * @throws IOException 如果发送失败
     */
    public void sendMessage(BroadcastMessage broadcastMessage) throws IOException {
        if (broadcastMessage != null) {
            sendMessage(broadcastMessage.getProtoMessage());
        }
    }
    
    /**
     * 关闭连接
     */
    public abstract void close();
    
    /**
     * 获取连接统计信息
     * 
     * @return 连接信息
     */
    public String getConnectionInfo() {
        return String.format("BattlePlayerConnection{playerId=%d, battleId=%d, teamId=%d, sessionId=%s, connected=%s}",
                           playerId, battleId, teamId, sessionId, connected);
    }
}
